home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club (Business) 1997 July / Software of the Month Club - Business (Volume 239) (July 1997).iso / pc / code / shared.dxr / 04477_MIDI Engine.ls < prev    next >
Encoding:
Text File  |  1996-02-21  |  7.2 KB  |  214 lines

  1. global gNotes, gCDNotes, gMidiInstruments, gMidiManager, gMidiStartTime, HARP, TIGO, HORN, DRUM, CHIME, BASS, gDonePlayingScript, gNotesToPlay, gInstrumentInCDTrack, gNextEnvLetter, gNextDIRFile, gMIDIPlayTRUE, gInitalized, noteObjs, windowsMIDIXObj, xNAVXOBJ, xCURXOBJ, gCPU, gfileSep, gRootPath, gHDpath, gXobjs, gXOBJPath, gCDpath, gCDName
  2.  
  3. on InitMIDILoop Instruments, NOTES
  4.   set C to count(Instruments)
  5.   set gMidiInstruments to [:]
  6.   sort(gMidiInstruments)
  7.   set gNotesToPlay to [:]
  8.   repeat with X = 1 to C
  9.     set InstrumentRef to getAt(Instruments, X)
  10.     set instrument to getaProp(gMidiManager, InstrumentRef)
  11.     set instruNotes to value(string(getaProp(NOTES, InstrumentRef)))
  12.     setaProp(gNotesToPlay, InstrumentRef, instruNotes)
  13.     set NoteTotal to count(instruNotes) / 3
  14.     set Data to [#ref: InstrumentRef, #action: #on, #time: 0, #NoteCount: 1, #NoteTotal: NoteTotal]
  15.     setaProp(gMidiInstruments, InstrumentRef, Data)
  16.     setaProp(instrument, #note, getAt(instruNotes, 2))
  17.     setaProp(instrument, #Pointer, 1)
  18.   end repeat
  19.   set gMIDIPlayTRUE to #true
  20.   set gMidiStartTime to the ticks
  21. end
  22.  
  23. on playAllRecordedTracksOrCD thenotes, Instruments, donePlayingScript
  24.   set gMIDIPlayTRUE to #true
  25.   if Instruments <> #ALL then
  26.     InitMIDILoop(Instruments, thenotes)
  27.   else
  28.     set InstrumentsList to []
  29.     repeat with X = 1 to 4
  30.       set int to getPropAt(thenotes, X)
  31.       add(InstrumentsList, int)
  32.     end repeat
  33.     InitMIDILoop(InstrumentsList, thenotes)
  34.   end if
  35.   if voidp(donePlayingScript) then
  36.     set gDonePlayingScript to #false
  37.   else
  38.     set gDonePlayingScript to donePlayingScript
  39.   end if
  40. end
  41.  
  42. on playOneTrack InstrumentRef, donePlayingScript
  43.   stopPlayAllTracks()
  44.   if getaProp(gNotes, InstrumentRef) <> [] then
  45.     InitMIDILoop(list(InstrumentRef), gNotes)
  46.     if voidp(donePlayingScript) then
  47.       set gDonePlayingScript to #false
  48.     else
  49.       set gDonePlayingScript to donePlayingScript
  50.     end if
  51.   else
  52.     do(donePlayingScript)
  53.   end if
  54. end
  55.  
  56. on playCD donePlayingScript
  57.   playAllRecordedTracksOrCD(gCDNotes, #ALL)
  58. end
  59.  
  60. on PLAYCDOFF
  61.   stopPlayAllTracks()
  62. end
  63.  
  64. on stopPlayAllTracks
  65.   repeat with Intrument in gMidiManager
  66.     set Channel to getaProp(Intrument, #Channel)
  67.     set note to getaProp(Intrument, #note)
  68.     set velocity to getaProp(Intrument, #velocity)
  69.     WriteMIDI(127 + Channel, note, velocity, Channel)
  70.     setaProp(Intrument, #Pointer, 0)
  71.   end repeat
  72.   set gMIDIPlayTRUE to #false
  73.   set gDonePlayingScript to #false
  74. end
  75.  
  76. on eraseRecording InstrumentRef
  77.   set instrument to getaProp(gMidiManager, InstrumentRef)
  78.   setaProp(gNotes, InstrumentRef, [])
  79.   setaProp(instrument, #Start, -1)
  80.   setaProp(instrument, #Pointer, 0)
  81.   setaProp(instrument, #stop, 0)
  82. end
  83.  
  84. on WriteMIDI code, note, velocity, Channel
  85.   if code = 192 then
  86.     exit
  87.   end if
  88.   if gCPU = #mpc then
  89.     set str to code && note && velocity
  90.     windowsMIDIXObj(mWrite, str)
  91.   else
  92.     if gCPU = #MAC then
  93.       set obj to getAt(noteObjs, Channel)
  94.       if code >= 144 then
  95.         obj(mNoteOn, note, velocity)
  96.       else
  97.         obj(mNoteOff, note)
  98.       end if
  99.     end if
  100.   end if
  101. end
  102.  
  103. on noteOn InstrumentRef, note, velocity
  104.   set TickLOC to the ticks
  105.   set InstrumentManager to getaProp(gMidiManager, InstrumentRef)
  106.   set Channel to getaProp(InstrumentManager, #Channel)
  107.   set StartfirstNote to getaProp(InstrumentManager, #Start)
  108.   set StopLastNote to getaProp(InstrumentManager, #stop)
  109.   if (note < 0) or (note > 127) then
  110.     put "Invalid Note:" && note
  111.     set note to 64
  112.   end if
  113.   WriteMIDI(143 + Channel, note, 127, Channel)
  114.   setaProp(InstrumentManager, #note, note)
  115.   if (getaProp(getaProp(gOBJECTS, InstrumentRef), #PlugInMixer) = #true) and (StopLastNote <> -1) then
  116.     if StartfirstNote = -1 then
  117.       setaProp(InstrumentManager, #Start, TickLOC)
  118.     end if
  119.     add(getaProp(gNotes, InstrumentRef), TickLOC - getaProp(InstrumentManager, #Start))
  120.     add(getaProp(gNotes, InstrumentRef), note)
  121.   end if
  122. end
  123.  
  124. on noteOff InstrumentRef, note
  125.   set TickLOC to the ticks
  126.   set InstrumentManager to getaProp(gMidiManager, InstrumentRef)
  127.   set Channel to getaProp(InstrumentManager, #Channel)
  128.   set StopLastNote to getaProp(InstrumentManager, #stop)
  129.   WriteMIDI(127 + Channel, getaProp(InstrumentManager, #note), 127, Channel)
  130.   if (getaProp(getaProp(gOBJECTS, InstrumentRef), #PlugInMixer) = #true) and (StopLastNote <> -1) then
  131.     set TimeOffset to TickLOC - getaProp(InstrumentManager, #Start)
  132.     add(getaProp(gNotes, InstrumentRef), TimeOffset)
  133.     setaProp(getaProp(gMidiManager, InstrumentRef), #stop, TickLOC)
  134.     if (TimeOffset > 1800) or (count(getaProp(gNotes, InstrumentRef)) > 179) then
  135.       setaProp(InstrumentManager, #stop, -1)
  136.     else
  137.       setaProp(InstrumentManager, #stop, TickLOC)
  138.     end if
  139.   end if
  140. end
  141.  
  142. on midiInterrupt
  143.   set Midilist to value(string(gMidiInstruments))
  144.   repeat with instrument in Midilist
  145.     set TickLOC to the ticks
  146.     if (getaProp(instrument, #time) + gMidiStartTime) > TickLOC then
  147.       next repeat
  148.     end if
  149.     if getaProp(instrument, #action) = #on then
  150.       MidiNoteON(getaProp(instrument, #ref), getaProp(gNotesToPlay, getaProp(instrument, #ref)))
  151.       next repeat
  152.     end if
  153.     MidiNoteOFF(getaProp(instrument, #ref), getaProp(gNotesToPlay, getaProp(instrument, #ref)))
  154.   end repeat
  155. end
  156.  
  157. on MidiNoteON IntrumentRef, NOTES
  158.   set Intrument to getaProp(gMidiManager, IntrumentRef)
  159.   set note to getaProp(Intrument, #note)
  160.   set Channel to getaProp(Intrument, #Channel)
  161.   WriteMIDI(143 + Channel, note, 127, Channel)
  162.   set InstanceIntrument to getaProp(gMidiInstruments, IntrumentRef)
  163.   setaProp(InstanceIntrument, #action, #off)
  164.   setaProp(InstanceIntrument, #time, getAt(NOTES, getaProp(Intrument, #Pointer) + 2))
  165. end
  166.  
  167. on MidiNoteOFF IntrumentRef, NOTES
  168.   set Intrument to getaProp(gMidiManager, IntrumentRef)
  169.   set note to getaProp(Intrument, #note)
  170.   set Channel to getaProp(Intrument, #Channel)
  171.   WriteMIDI(127 + Channel, note, 127, Channel)
  172.   set InstanceIntrument to getaProp(gMidiInstruments, IntrumentRef)
  173.   set NoteCount to getaProp(InstanceIntrument, #NoteCount)
  174.   if NoteCount >= getaProp(InstanceIntrument, #NoteTotal) then
  175.     deleteProp(gMidiInstruments, IntrumentRef)
  176.     setaProp(getaProp(gMidiManager, IntrumentRef), #Pointer, -1)
  177.     if count(gMidiInstruments) = 0 then
  178.       set gMIDIPlayTRUE to #false
  179.     end if
  180.   else
  181.     set Pointer to getaProp(Intrument, #Pointer) + 3
  182.     setaProp(InstanceIntrument, #NoteCount, NoteCount + 1)
  183.     setaProp(InstanceIntrument, #action, #on)
  184.     setaProp(InstanceIntrument, #time, getAt(NOTES, Pointer))
  185.     setaProp(getaProp(gMidiManager, IntrumentRef), #Pointer, Pointer)
  186.     setaProp(getaProp(gMidiManager, IntrumentRef), #note, getAt(NOTES, Pointer + 1))
  187.   end if
  188. end
  189.  
  190. on allNotesOff
  191.   if gCPU = #mpc then
  192.     repeat with instrument = 1 to 6
  193.       repeat with note = 0 to 127
  194.         set str to 128 + (instrument - 1) && note && 0
  195.         windowsMIDIXObj(mWrite, str)
  196.       end repeat
  197.     end repeat
  198.   else
  199.     if gCPU = #MAC then
  200.       repeat with instrument = 1 to 6
  201.         set obj to getAt(noteObjs, instrument)
  202.         if objectp(obj) then
  203.           repeat with Xnote = 0 to 127
  204.             obj(mNoteOff, Xnote)
  205.             set NOW to the timer
  206.             repeat while the timer < (NOW + 0.00001)
  207.             end repeat
  208.           end repeat
  209.         end if
  210.       end repeat
  211.     end if
  212.   end if
  213. end
  214.